<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CommentController extends Controller
{
    /*添加评论*/
    public function postAdd(Request $request) {  
        // dd($request -> all());
        // 表单验证
        $this ->  validate($request,[
           'gid' => 'required',
           'eval' => 'required',
           'content' => 'required'
        ],[
            'eval.required' => '请选择评价',
        ]);

        // 获得订单详情id
        $did = $request -> input('did');

        // 获得订单id
        $oid = $request -> input('oid');

        // 获取商品评论的表单信息,去掉token,订单详情id,订单id
        $date = $request -> except('_token','did','oid');
        $date['uid'] = session('h_user')['h_uid'];
        $date['time'] = time();

        
        // 更新评论表,并获得评论id
        $commentid = DB::table('comment') -> insertGetId($date);
        // 更新评论详情表
        DB::table('order_detail') -> where('did',$did) -> update(['commented'=>$date['eval'],'commentid'=>$commentid]);

        if($commentid){            
            // return back() -> with('success','评论成功');
            return redirect("/home/order/detail?id=".$oid) -> with('success','评论成功');
        }else{            
            $request -> flash();
            return back() -> with('error','评论失败');
        }
    }

    /*显示评论 在商品详情页显示*/
    public function getShow(Request $request) {
        $pingjia = ['1' => '好评','2' => '中评','3' => '差评'];

        // 获得评论表和用户表信息
        $date = DB::table('comment') -> join('home_user','comment.uid','=','home_user.h_uid') -> where('gid',$request -> input('id')) -> orderBy('time', 'desc') -> paginate(10) ;
        // 拼接返回信息
        $res = "<table border='0' class='jud_list' style='width:100%; margin-top:30px;' cellspacing='0' cellpadding='0' id='comment_table'>";
        foreach($date as $k => $v){
            $res .= "<tr valign='top'>
                        <td width='160'><img src='".$v['h_upic']."' width='20' height='20' align='absmiddle' />&nbsp;".$v['h_uname']."</td>
                        <td width='180'>
                            评价：<font color='#999999'>".$pingjia[$v['eval']]."</font> <br />
                        </td>
                        <td>
                            ".$v['content']." <br />
                            <font color='#999999'>". date('Y-m-d H:i:s',$v['time']) ."</font>
                        </td>
                    </tr>";
        } 
           
        echo $res."</table><div class='pages'>".$date -> appends(['id' => $request -> input('id')]) -> render()."</div>";
    }

    /*获得order_detail页的评论内容*/
    public function postContent(Request $request){
        // 获得评论id
        $id = $request -> input('id');
        // 查询评论内容
        $res = DB::table('comment') -> where('id',$id) -> first()['content'];
        // 打印评论内容
        echo $res;
    }

    /*评论列表*/
    public function getCommentlist(){
        // 获得用户id
        $h_uid = session('h_user')['h_uid'];
        // 评价的显示,调用后可将对应的好中差评转化成对应的文字表示
        $pingjia = ['1' => '好评','2' => '中评','3' => '差评'];

        // 获得商品对应的所有的评论
        $data = DB::table('order_detail') ->join('goods','order_detail.gid','=','goods.gid') -> where('h_uid',session('h_user')['h_uid'])-> orderBy('did','desc')->paginate(15);
        
        // 引用模板
        return view('home/comment/commentlist',['data'=>$data,'pingjia'=>$pingjia]);
    }

}
